package com.mmlc.meimei.lichengkongjian.classify;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.androidnetframe.utils.LogUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.HackyViewPager;
import com.mmlc.utils.DownloadUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by OutSide on 2017/1/1.
 */

public class ActivityPhotoView extends InterActivity {


    ImageViewPagerAdapter adapter;

    HackyViewPager pager;

    private int pagerPosition;

    private TextView indicator;
    int postion = 0;
    List<String> list=new ArrayList<>();

    @Override
    protected int initContentView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        return R.layout.activity_photoview;
    }

    @Override
    protected void initView() {
        pager = (HackyViewPager) findViewById(R.id.pager);
        indicator = (TextView) findViewById(R.id.indicator);
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownloadUtils downloadUtils=new DownloadUtils(context);
                downloadUtils.downloadAPK(list.get(postion),System.currentTimeMillis()+".jpg");
            }
        });
        Bundle bundle = getIntent().getBundleExtra("bundle");
        String index = bundle.getString("index");
        if (!TextUtils.isEmpty(index))
            postion = Integer.parseInt(index);
        LogUtils.e("INDEX", index);
        list = bundle.getStringArrayList("imgs");
        LogUtils.e("list", list.size());
        adapter = new ImageViewPagerAdapter(getSupportFragmentManager(), list);
        pager.setAdapter(adapter);
        if (!TextUtils.isEmpty(index)) {
            pager.setCurrentItem(Integer.parseInt(index));
        }
        CharSequence text = getString(R.string.viewpager_indicator, Integer.parseInt(index) + 1, pager.getAdapter().getCount());
        indicator.setText(text);
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                CharSequence text = getString(R.string.viewpager_indicator, position + 1, pager.getAdapter().getCount());
                indicator.setText(text);
                ActivityPhotoView.this.postion=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        pager.setOnTouchListener(new View.OnTouchListener() {

            int flag = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        flag = 0;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        flag = 1;
                        break;
                    case MotionEvent.ACTION_UP:
                        if (flag == 0) {
                            finish();
                        }
                        break;


                }
                return false;
            }
        });
    }


    public class ImageViewPagerAdapter extends FragmentStatePagerAdapter {
        private static final String IMAGE_URL = "image";

        List<String> mDatas;

        public ImageViewPagerAdapter(FragmentManager fm, List data) {
            super(fm);
            mDatas = data;
        }

        @Override
        public Fragment getItem(int position) {
            String url = mDatas.get(position);
            Fragment fragment = ImageFragment.newInstance(url);
            return fragment;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }
    }

}
